package cn.lili.controller.feign.system;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.lili.common.utils.BeanUtil;
import cn.lili.modules.system.client.DictionaryClient;
import cn.lili.modules.system.client.PackConfigClient;
import cn.lili.modules.system.constants.DictConstant;
import cn.lili.modules.system.entity.dos.Dictionary;
import cn.lili.modules.system.entity.dos.PackConfig;
import cn.lili.modules.system.entity.vo.PackConfigVO;
import cn.lili.modules.system.service.PackConfigService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class PackConfigFeignController implements PackConfigClient {

    @Resource
    private PackConfigService packConfigService;

    @Resource
    private DictionaryClient dictionaryClient;

    @Override
    public PackConfigVO getById(String packId) {
        PackConfigVO result = new PackConfigVO();
        PackConfig packConfig = packConfigService.getById(packId);
        if (packConfig != null) {
            List<Dictionary> clientListByCode = dictionaryClient.getListByCode(DictConstant.PACK_CONFIG_NAME);
            BeanUtil.copyProperties(packConfig, result);
            if (CollectionUtil.isNotEmpty(clientListByCode)) {
                for (Dictionary dictionary : clientListByCode) {
                    if (dictionary.getId().equals(result.getDictId())) {
                        result.setDictName(dictionary.getValueName());
                        result.setDictVal(StrUtil.isNotEmpty(dictionary.getValue()) ? dictionary.getValue() : null);
                    }
                }
            }
        }
        return result;
    }


    /**
     * 获取配置数据
     * @param field 字段
     * @param value 字段值
     * @return 配置数据
     */
    @Override
    public List<PackConfig> getPackConfigByField(String field, String value) {
        QueryWrapper<PackConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(field, value);
        return packConfigService.list(queryWrapper);
    }

    /**
     * 根据packids查询信息
     * @param packIds
     * @return
     */
    @Override
    public List<PackConfig> getPackConfigByPackIds(List<String> packIds){
        return packConfigService.getPackConfigByPackIds(packIds);
    }

}
